package myDaos;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.entities.StocksMatchingInfo;

@Stateless
public class StocksMatchingDao {
	@PersistenceContext(unitName = "UnileverProcedure")
    private EntityManager em;
	
	public EntityManager getEntityManager() {
        return em;
    }
	
	public Boolean callMatchingReportProcedure(String p_country,String p_distributor_no,Date p_from_date,Date p_to_date){
		Boolean isCalled;
		try {
		    Query q = getEntityManager().createNativeQuery("{call prepare_stocks_matching_report(:country, :distributor, :from_date, :to_date)}").setParameter("country",p_country).setParameter("distributor",p_distributor_no).setParameter("from_date",p_from_date).setParameter("to_date",p_to_date);
		    q.executeUpdate();
		    isCalled=true;
	} catch (Exception e) {
		e.printStackTrace();
		isCalled=false;
	}
		return isCalled;
	}
	
	public List<StocksMatchingInfo> getMatchingReportList(){
		List<StocksMatchingInfo> smlist=null;
		try {
			StringBuffer queryBiffer = new StringBuffer();
			queryBiffer.append(" SELECT s FROM StocksMatchingInfo s "); 

			Query q = getEntityManager().createQuery(queryBiffer.toString());
			
			smlist = q.getResultList();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return smlist;
	}
	
}
